package com.yy.wga.mp.module.goods.task;


import com.yy.wga.mp.common.interceptor.RequestUserHolder;
import com.yy.wga.mp.common.utils.common.StringUtils;
import com.yy.wga.mp.module.merchant.entity.pojo.MerchantInfoDO;
import com.yy.wga.mp.module.merchant.service.MerchantInfoService;
import com.yy.wga.mp.module.promoter.api.response.bind.GetPromoterRegisterAndBindStatusResp;
import com.yy.wga.mp.module.promoter.service.IPromoterService;
import com.yy.wga.mp.module.user.entity.pojo.UserWechatDO;
import com.yy.wga.mp.module.user.service.UserWechatService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Component
public class SyncWechatPromoteSharerAppIdTask {

    @Resource
    private UserWechatService wechatService;
    @Resource
    private MerchantInfoService merchantInfoService;
    @Resource
    private IPromoterService promoterService;

//    @Scheduled(cron = "0 0/30 * * * ? ")
    public void syncWechatPromoteSharerAppIdTask() {

        List<MerchantInfoDO> merchantInfoDOList = merchantInfoService.list();
        for (MerchantInfoDO merchantInfoDO : merchantInfoDOList) {

            String appKey = merchantInfoDO.getAppKey();
            BaseRequestDTO baseRequestDTO = new BaseRequestDTO();
            baseRequestDTO.setAppKey(appKey);
            RequestUserHolder.setBaseRequest(baseRequestDTO);

            List<UserWechatDO> wechatDOList = wechatService.lambdaQuery()
                    .eq(UserWechatDO::getAppKey, appKey)
                    .eq(UserWechatDO::getDeleted, 0)
                    .eq(UserWechatDO::getSharerAppid, "")
                    .list();
            for (UserWechatDO userWechat : wechatDOList) {
                if (StringUtils.isBlank(userWechat.getSharerAppid())) {
                    GetPromoterRegisterAndBindStatusResp statusResp = promoterService.getPromoterRegisterAndBindStatus(userWechat.getOpenId());
                    String sharerAppid = statusResp.getSharerAppid();
                    userWechat.setSharerAppid(sharerAppid);
                    wechatService.updateById(userWechat);
                }
            }
            RequestUserHolder.setBaseRequest(null);
        }
    }

}
